package com.hainatong.shiphelper.module.holiday.fragment;


import com.base.common.utils.UIUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.shiphelper.MvvmLazyFragment;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.FragmentRecyclerviewBinding;
import com.hainatong.shiphelper.module.holiday.adapter.HolidayHistoryAdapter;
import com.hainatong.shiphelper.module.holiday.viewmodel.VacationViewModel;
import com.hainatong.shiphelper.widget.recycleviewdriver.ListItemDecoration;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;

import android.os.Bundle;
import android.view.View;
import androidx.recyclerview.widget.LinearLayoutManager;

/**
 *  申请 休假人的 申请历史
 */
public class VactionHistoryFragment extends MvvmLazyFragment<FragmentRecyclerviewBinding, VacationViewModel>
{
    private static String APPLY_HISTORY_TAG = "HolidayApplyResultFragment";
    //UserVacationBean
    private String workerUuid = "";;

    private HolidayHistoryAdapter adapter;

    public static VactionHistoryFragment newInstance(String typeCode)
    {
        Bundle bundle = new Bundle();
        bundle.putString( APPLY_HISTORY_TAG , typeCode );
        VactionHistoryFragment applyFragment = new VactionHistoryFragment();
        applyFragment.setArguments( bundle );
        return applyFragment;
    }

    @Override
    protected void initParameters()
    {
        Bundle args = getArguments();

        if(  args != null )
        {
            workerUuid = args.getString(APPLY_HISTORY_TAG , "");
        }

    }


    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_recyclerview;
    }

    @Override
    public int getBindingVariable()
    {
        return 0;
    }

    @Override
    protected void onRetryBtnClick() {

    }

    @Override
    protected void initView(View view)
    {
        adapter = new HolidayHistoryAdapter();
        viewDataBinding.recyclerview.addItemDecoration(new ListItemDecoration(SizeUtils.dp2px(10)));
        viewDataBinding.recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
        viewDataBinding.recyclerview.setHasFixedSize(true);
        viewDataBinding.recyclerview.setAdapter( adapter );

        viewDataBinding.refreshlayout.setRefreshHeader( new ClassicsHeader(getActivity()));
        viewDataBinding.refreshlayout.setRefreshFooter( new ClassicsFooter(getActivity()));
        viewDataBinding.refreshlayout.setOnRefreshListener(refreshLayout -> {
            viewModel.tryRefreshVacHistory();
        });
        viewDataBinding.refreshlayout.setOnLoadMoreListener(refreshLayout -> {
            viewModel.loadMoreVacHistory();
        });
        // 请假详情
        adapter.setOnItemClickListener((adapter1, view1, position) -> {

            //UserVacationBean vacation = adapter.getItem( position );
            //ComFragmentActivity.startViewByParam(getActivity(), Constants.VIEW_HOLIDAY_APPLY_DETAIL , GsonUtil.obj2json(vacation));
        });

    }

    @Override
    protected void lazyLoad()
    {
        // 查 休假人 休假历史
        if(UIUtils.isNotEmpty( workerUuid ))
        {
            viewModel.getVacHistoryByUserId( workerUuid );
        }
        // list 结果
        viewModel.getVacationList().observe(this,result -> {
            if( result.isSuccess() )
            {
                if( result.isRefresh() )
                {
                    viewDataBinding.refreshlayout.finishRefresh(true);
                    if( result.getData() != null )
                    {
                        adapter.setList(result.getData());
                    }
                }
                else
                {
                    viewDataBinding.refreshlayout.finishLoadMore(true);
                    if( result.getData() != null )
                    {
                        adapter.addData(result.getData());
                    }
                }
            }
            else
            {
                if( result.isRefresh() )
                {
                    //刷新时 加载失败
                    ToastUtils.showShort( result.getMsg() );
                }
                else
                {
                    if( result.isLoadMoreEmpty() )
                    {
                        viewDataBinding.refreshlayout.finishLoadMoreWithNoMoreData();
                    }
                }
            }
        });

    }
}
